#include "formpeople.h"
#include "ui_formpeople.h"
#include "dlgtenantedit.h"
#include "dlgfammemberedit.h"
#include <QDebug>


FormPeople::FormPeople(QWidget *parent) :
    QWidget(parent),
    mp_ten_model(NULL),
    mp_fam_model(NULL),
    ui(new Ui::FormPeople)
{
    ui->setupUi(this);

    connect(ui->tv_tenants, SIGNAL(activated(QModelIndex)), this, SLOT(tenant_activated(QModelIndex)));
    connect(ui->tv_family, SIGNAL(activated(QModelIndex)), this, SLOT(fam_member_activated(QModelIndex)));
    connect(ui->btn_new_ten, SIGNAL(clicked()), this,SLOT(new_tenant()));
    connect(ui->btn_del_ten, SIGNAL(clicked()), this, SLOT(del_tenant()));

    connect(ui->btn_new_fm, SIGNAL(clicked()), this, SLOT(new_fam_member()));
    connect(ui->btn_del_fm, SIGNAL(clicked()), this, SLOT(del_fam_member()));

    connect(ui->btn_select, SIGNAL(clicked()), this, SLOT(select_record()));

    ui->btn_select->hide();
}

FormPeople::~FormPeople()
{
    delete ui;
}

void FormPeople::update_models(bool do_not_load)
{
    ui->tv_tenants->setModel(NULL);
    if(!do_not_load)
        mp_ten_model->load_data();
    ui->tv_tenants->setModel(mp_ten_model);
    mp_tenant_sel_model = ui->tv_tenants->selectionModel();
    connect(mp_tenant_sel_model, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(tenant_selected(QModelIndex)));
}


void FormPeople::tenant_selected(const QModelIndex &index)
{
    mqv_current_t_index = mp_ten_model->get_db_index(index);

    update_fam_model();
}

void FormPeople::tenant_activated(const QModelIndex &index)
{

    Q_UNUSED(index);
    SLEE_HDB_REC qh_data = mp_dbms->get_data(LAKDB_TENANTS, mqv_current_t_index);
    DlgTenantEdit *p_te = new DlgTenantEdit();
    p_te->set_dbms(mp_dbms);
    p_te->set_data(qh_data);
    if(p_te->exec())
    {
        if(p_te->is_changed())
        {
            qh_data = p_te->get_data();
            mp_dbms->db_put(LAKDB_TENANTS, qh_data, mqv_current_t_index);
            mp_ten_model->update_data(mqv_current_t_index);
            ui->tv_tenants->setVisible(false);
            ui->tv_tenants->setVisible(true);
            lakhMsg()<<RUSTR("Данные по нанимателям обновлены.");
        }
    }
    delete p_te;
}

void FormPeople::new_tenant()
{
    DlgTenantEdit *p_te = new DlgTenantEdit();
    SLEE_HDB_REC qh_data;
    p_te->set_dbms(mp_dbms);
    p_te->set_data(qh_data);
    if(p_te->exec())
    {
        if(p_te->is_changed())
        {
            qh_data = p_te->get_data();
            mp_dbms->db_put(LAKDB_TENANTS, qh_data);
            update_models();
            lakhMsg()<<RUSTR("Данные по нанимателю внесены в БД.");
        }
    }
    delete p_te;
}

void FormPeople::del_tenant()
{
        if(!mqv_current_t_index.isValid())
        {
            lakhMsgE()<<RUSTR("Ошибка! Не выбран наниматель");
            return ;
        }
        if(!mp_dbms->can_remove(LAKDB_TENANTS,mqv_current_t_index)) return;
        SLEE_HDB_REC qh_data = mp_dbms->get_data(LAKDB_TENANTS, mqv_current_t_index);
        mp_dbms->get_db(LAKDB_TENANTS)->removeData(qh_data, mqv_current_t_index);
        lakhMsg() <<RUSTR("Данные по нанимателю, были удалены из БД.");
        mqv_current_t_index = QVariant();
        update_models();
}


void FormPeople::update_fam_model()
{
    ui->tv_family->setModel(NULL);
    mqv_current_f_index = QVariant();
    if(mqv_current_t_index.isNull()) return;
    mp_fam_model->load_data(mqv_current_t_index);
    ui->tv_family->setModel(mp_fam_model);
    mp_fam_sel_model = ui->tv_family->selectionModel();
    connect(mp_fam_sel_model, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(fam_member_selected(QModelIndex)));
}


void FormPeople::fam_member_selected(const QModelIndex &index)
{
    mqv_current_f_index = mp_fam_model->get_db_index(index);
}

void FormPeople::fam_member_activated(const QModelIndex &index)
{
    Q_UNUSED(index);
    SLEE_HDB_REC qh_data;
    qh_data = mp_fam_model->get_data(mqv_current_f_index);
    DlgFamMemberEdit *p_fe = new DlgFamMemberEdit();
    p_fe->set_dbms(mp_dbms);
    p_fe->set_data(qh_data);
    if(p_fe->exec())
    {
        if(p_fe->is_changed())
        {
            qh_data = p_fe->get_data();
            mp_dbms->db_put(LAKDB_FAMILIES, qh_data, mqv_current_f_index);
            mp_fam_model->update_data(mqv_current_f_index);
            ui->tv_family->setVisible(false);
            ui->tv_family->setVisible(true);
            lakhMsg()<<RUSTR("Данные по членам семьи обновлены.");
        }
    }

    delete p_fe;
}

void FormPeople::new_fam_member()
{
        if(mqv_current_t_index.isNull())
        {
            lakhMsgE()<<RUSTR("Ошибка. Не выбран наниматель.");
            return;
        }
        SLEE_HDB_REC qh_data;
        qh_data[FLD_FAM_TINDEX] = mqv_current_t_index;
        DlgFamMemberEdit *p_fe = new DlgFamMemberEdit();
        p_fe->set_dbms(mp_dbms);
        p_fe->set_data(qh_data);
        if(p_fe->exec())
        {
            if(p_fe->is_changed())
            {
                qh_data = p_fe->get_data();
                mp_dbms->db_put(LAKDB_FAMILIES, qh_data);
                update_fam_model();
                lakhMsg()<<RUSTR("Новый член семьи нанимателя добавлен в БД.");
            }
        }
        delete p_fe;
}

void FormPeople::del_fam_member()
{
    if(mqv_current_f_index.isNull())
    {
        lakhMsgE()<<RUSTR("Ошибка. Не выбран член семьи нанимателя");
        return;
    }
    if(!mp_dbms->can_remove(LAKDB_FAMILIES, mqv_current_f_index)) return;
    SLEE_HDB_REC qh_data = mp_fam_model->get_data(mqv_current_f_index);
    mp_dbms->get_db(LAKDB_FAMILIES)->removeData(qh_data, mqv_current_f_index);
    update_fam_model();
    lakhMsg()<<RUSTR("Данные по член семьи нанимателя из БД удалены.");
}


void FormPeople::select_record()
{
    emit record_selected(mqv_current_t_index);
    hide();
}

void FormPeople::enable_selection()
{
    ui->btn_select->show();
}


void FormPeople::set_tenant_selected(const QVariant &qv_t_index)
{
    QModelIndex mod_index = mp_ten_model->get_model_index(qv_t_index);
    if(!mod_index.isValid()) return;
    mp_tenant_sel_model->select(mod_index, QItemSelectionModel::Select);
    ui->tv_tenants->scrollTo(mod_index, QAbstractItemView::PositionAtCenter);
    tenant_selected(mod_index);
}


void FormPeople::hide_sel_btn()
{
    ui->btn_select->hide();
}
